package com.woniu.tms.openclass.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.Course;
import com.woniu.tms.entity.Room;
import com.woniu.tms.entity.UserRole;
import com.woniu.tms.openclass.service.openClassService;
import com.woniu.tms.openclass.service.openClassUserService;

@Controller
@RequestMapping("class")
public class openClassController {
       @Autowired
       openClassService classService;
       @Autowired
       openClassUserService userService;
       
	   
      @RequestMapping("supervisorIndexDo")
      @ResponseBody
       public Map<String,Object> supervisorIndexDo(@RequestParam(name="pageNum", defaultValue ="1") Integer pageNum,@RequestParam(name="pageSize",defaultValue="4") Integer pageSize, String rnameLike){
    	  Map<String,Object> map=new HashMap<String,Object>();
    	   try {
    		   //设置分页的开始
    		   PageHelper.startPage(pageNum, pageSize);
    		   //根据条件的查询
    		   List<Classes> classesList = classService.findByRnameLike(rnameLike);
    	
    		   PageInfo<Classes> pageInfo=new PageInfo<Classes>(classesList);
    		  
    		   map.put("classesList", classesList);
    		   map.put("pageInfo", pageInfo);
    		   map.put("msg", true);
    	   }catch(Exception e) {
    		   map.put("msg", false);
    		      e.printStackTrace();
    	   }
    	  return map;
    	  
      }   
       
      

      
      @RequestMapping("teacherIndexDo")
      @ResponseBody
      public  Map<String,Object> teacherIndexDo(Integer uid){
    	  Map<String,Object> map=new HashMap<String,Object>();
    	     try {
    	    	 List<Classes> classList=classService.findByUid(uid);
    	    	 System.out.println(classList+"111");
    	    	 PageInfo<Classes> pageInfo=new PageInfo<Classes>(classList);
    	      
    	    	 
    	    	 map.put("pageInfo", pageInfo);
    	    	 map.put("msg", true);
    	    	 map.put("classList", classList);
    	     }catch(Exception e) {
    	    	  map.put("msg", false);
    	    	  e.printStackTrace();
    	     }
    	      
    	  
    	    
    	            
		return map;
    	    
      }
       
       
   	@RequestMapping("supervisorIndex")
   	public String supervisorIndex() {
   		
     return "WEB-INF/openClass/clbum/supervisorIndex";
   	}
   	
   	@RequestMapping("teacherIndex")
   	public String teacherIndex() {
   		
     return "WEB-INF/openClass/clbum/teacherIndex";
   	}
   	
   
   	
   	
   	
   	@RequestMapping("add")
   	public String add() {
   		
   		 return "WEB-INF/openClass/clbum/add";
   	}
   	
   	@RequestMapping("addDo")
   	@ResponseBody
   	public Map<String,Object>  addDo(Classes classes){
   		Map<String,Object>  map=new HashMap<>();
   		try {
   			
   			classService.add(classes);
   			map.put("msg", true);
   			
   		}catch(Exception e) {
   			 map.put("msg", false);
   			 e.printStackTrace();
   		}
   	
   		
		return map;
   		
   	}
   	
   	@RequestMapping("deleteDo")
   	@ResponseBody
   	public Map<String,Object> deleteDo(Integer[] cid){
   		Map<String,Object> map=new HashMap<>();
   		  try {
   			classService.delete(cid);
   			map.put("msg", true);
   		  }catch(Exception e) {
   			   map.put("msg", false);
   		  }
   		   
		return map;
   		
   	}
   	
   	@RequestMapping("list")
    @ResponseBody
    public Map<String,Object>  list() {
 	         
 	   Map<String,Object> map=new HashMap<>();
 	   
 	         try {
 	        	 List<UserRole> userRoleList=userService.findAll();  
 	             
 	              List<UserRole> teacherList=new ArrayList<UserRole>();
 	               for (UserRole userRole : userRoleList) {
 					     if(userRole.getRid()==3) {
 					    	 teacherList.add(userRole);
 					     }
 				}
 	                
 	               List<UserRole> classManagerList=new ArrayList<>();
 	               for (UserRole userRole : userRoleList) {
 					     if(userRole.getRid()==2) {
 					    	 classManagerList.add(userRole);
 					     }
 				}
 	               
 	             
 	               
 	              
 	               List<Course> courseList=userService.findCourse();
 	              
 	               
 	               List<Room> roomList= userService.findRoom();
 	               
 	               
 	              
 	                  System.out.println(teacherList.size());
 	                map.put("roomList", roomList);
 	                map.put("courseList", courseList);
 	                map.put("teacherList", teacherList);
 	                map.put("classManagerList", classManagerList);
 	                map.put("msg",true);
 	         }catch(Exception e) {
 	        	  map.put("mgs", false);
 	        	   e.printStackTrace();
 	         }
         
             
             
		     return map;
 	   
    }
   	
	@RequestMapping("classTeacherIndex")
   	public String classTeacherIndex() {
   		  return "WEB-INF/openClass/clbum/classTeacher";
   	}
   	
   
   @RequestMapping("classTeacherIndexDo")
   @ResponseBody
   public  Map<String,Object> classTeacherIndexDo(Integer uid){
 	  Map<String,Object> map=new HashMap<String,Object>();
 	     try {
 	    	 List<Classes> classList=classService.findByUids(uid);
 	    	 
 	    	 PageInfo<Classes> pageInfo=new PageInfo<Classes>(classList);
 	      
 	    	 
 	    	 map.put("pageInfo", pageInfo);
 	    	 map.put("msg", true);
 	    	 map.put("classList", classList);
 	     }catch(Exception e) {
 	    	  map.put("msg", false);
 	    	  e.printStackTrace();
 	     }
 	      
 	  
 	    
 	            
		return map;
 	    
   }
}
